package com.chf.job.controller;

import com.chf.job.common.Const;
import com.chf.job.common.ServerResponse;
import com.chf.job.pojo.Company;
import com.chf.job.pojo.Product;
import com.chf.job.pojo.User;
import com.chf.job.service.CompanyService;
import com.chf.job.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CompanyService companyService;
    /**
     * 添加公司产品信息
     */
    @PostMapping("product/addProduct.html")
    @ResponseBody
    public ServerResponse addProduct(HttpServletRequest request, Product product) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Company company = companyService.getByEmail(user.getEmail());
        int result = productService.addProduct(company.getId(), product);
        if (result > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.ADD_COMPANY);
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }
}
